package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.CustomerQueryObject;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    //@RequiresPermissions(value = {"customer:potentialList","潜在客户管理页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        qo.setStatus(Customer.STATUS_COMMON);

        //Subject subject = SecurityUtils.getSubject();
       /* if (!subject.hasRole("ADMIN")||subject.hasRole("Market_Manager")) {
            Employee employee = (Employee) subject.getPrincipal();
            qo.setSellerId(employee.getId());
        }*/
        PageInfo<Customer> result = customerService.query(qo);
        model.addAttribute("result", result);

        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market_Manager");
        model.addAttribute("sellers",sellers);

        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByParenetSn("job");
        model.addAttribute("jobs",jobs);
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectByParenetSn("source");
        model.addAttribute("sources",sources);
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectByParenetSn("communicationMethod");
        model.addAttribute("ccts",ccts);

        return "customer/potentialList";
    }

    @RequestMapping("/delete")
   // @RequiresPermissions(value = {"customer:delete","潜在客户管理删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    //@RequiresPermissions(value = {"customer:saveOrUpdate","潜在客户管理新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        if (customer.getId() != null) {
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonResult();
    }
}
